<template>
    <el-select v-model="sval" 
        :clearable="clearable"
        :placeholder="placeholder"
         :style="{width}" 
         @change="onChcange">
        <el-option v-for="dt in selectList" :key="dt.value" :value="dt.value" :label="dt.label" />
    </el-select>
</template>
<script setup>
import {ref,onMounted} from "vue"
import {getSelectList} from '@/api/select'
import { useUserStore } from '@/pinia/modules/user'
const userStore = useUserStore()

const props = defineProps({
    modelValue:'',
    width:{
        type:String,
        default:'100%'
    },
    params:{  // 查询条件
        type:Object,
        default:{}
    },
    clearable:{
        type:Boolean,
        default:true
    },
    placeholder:{
        type:String,
        default:'请选择'
    }
})
const emit = defineEmits(['update:modelValue','change'])
const sval = ref('')
const selectList = ref([])

onMounted(()=>{ 
    getDataList()
})

const getDataList = async () => {
    const res = await getSelectList({ 
        page: 1, 
        limit: 200,
        companyId:userStore.userInfo.companyId,
        farmId:userStore.userInfo.farmId,
        ...props.params
    })
  if (res) {
    selectList.value=res
  }
}

const onChcange = ()=>{
    emit('update:modelValue',sval.value) 
    emit('change',selectList.value.find(item=>item.value == sval.value) ) 
}

</script>

<style scoped lang="scss">
</style>